from PyQt5.QtWidgets import QLabel, QLineEdit,QPushButton, QVBoxLayout, QWidget, QHBoxLayout, \
                            QFrame, QDoubleSpinBox, QRadioButton
import define
from config.config_mgr import get_config_manager
from signal_mgr import get_signal_manager
class CSelectionView(QWidget):
    """右侧筛选加排序控制面板"""
    def __init__(self):
        super().__init__()
        self._init_widget()
        self._init_data()
        self._init_connect()

    def _init_widget(self):
        """初始化控件"""
        vbox = QVBoxLayout(self)
        self.top_select_panel = QFrame() # 上面筛选面板
        self.middle_sort_panel = QFrame() # 中间排序面板
        self.bottom_submit_button = QPushButton('更新') # 下面确认按钮
        vbox.addWidget(self.top_select_panel)
        vbox.addWidget(self.middle_sort_panel)
        vbox.addWidget(self.bottom_submit_button)
        vbox.addStretch(1)
        self._init_top()
        self._init_middle()

    def _init_connect(self):
        """初始化信号"""
        self.bottom_submit_button.clicked.connect(self._update_config)


    def _update_config(self):
        """刷新"""
        config_mgr = get_config_manager()
        config_mgr.show_number = int(self.lineedit_item_number.value())
        config_mgr.word = self.lineedit_select_word.text()
        config_mgr.function_consuming_time = [float(self.spinbox_function_consuming_time1.value()), float(self.spinbox_function_consuming_time2.value())]
        config_mgr.sentence_max_consuming_time = [float(self.spinbox_sentence_max_consuming_time1.value()), float(self.spinbox_sentence_max_consuming_time2.value())]
        config_mgr.sentence_max_time = [float(self.spinbox_sentence_max_time1.value()), float(self.spinbox_sentence_max_time2.value())]
        if self.radio_function_consuming_time.isChecked():
            config_mgr.sort_type = define.SORT_FUNCTION_CONSUMING_TIME
        elif self.radio_sentence_max_consuming_time.isChecked():
            config_mgr.sort_type = define.SORT_SENTENCE_MAX_CONSUMING_TIME
        elif self.radio_sentence_max_time.isChecked():
            config_mgr.sort_type = define.SORT_SENTENCE_MAX_TIME
        get_signal_manager().SIGNAL_CHECK.emit()

    def _init_data(self):
        """初始化数据"""
        config_mgr = get_config_manager()
        if config_mgr.show_number:
            self.lineedit_item_number.setValue(config_mgr.show_number)
        else:
            self.lineedit_item_number.setValue(20)
        if config_mgr.word:
            self.lineedit_select_word.setText(config_mgr.word)
        if config_mgr.function_consuming_time:
            self.spinbox_function_consuming_time1.setValue(config_mgr.function_consuming_time[0])
            self.spinbox_function_consuming_time2.setValue(config_mgr.function_consuming_time[1])
        if config_mgr.sentence_max_consuming_time:
            self.spinbox_sentence_max_consuming_time1.setValue(config_mgr.sentence_max_consuming_time[0])
            self.spinbox_sentence_max_consuming_time2.setValue(config_mgr.sentence_max_consuming_time[1])
        if config_mgr.sentence_max_time:
            self.spinbox_sentence_max_time1.setValue(config_mgr.sentence_max_time[0])
            self.spinbox_sentence_max_time2.setValue(config_mgr.sentence_max_time[1])
        if config_mgr.sort_type:
            if config_mgr.sort_type == define.SORT_FUNCTION_CONSUMING_TIME:
                self.radio_function_consuming_time.setChecked(True)
            elif config_mgr.sort_type == define.SORT_SENTENCE_MAX_CONSUMING_TIME:
                self.radio_sentence_max_consuming_time.setChecked(True)
            elif config_mgr.sort_type == define.SORT_SENTENCE_MAX_TIME:
                self.radio_sentence_max_time.setChecked(True)

    def _init_top(self):
        """初始化筛选面板"""
        vbox = QVBoxLayout(self.top_select_panel)
        w0 = QWidget(self.top_select_panel)
        w01 = QWidget(self.top_select_panel)
        w1 = QWidget(self.top_select_panel)
        w2 = QWidget(self.top_select_panel)
        w3 = QWidget(self.top_select_panel)
        
        label_title_top = QLabel('筛选')
        vbox.addWidget(label_title_top)  

        vbox.addWidget(w0)
        vbox.addWidget(w01)
        vbox.addWidget(w1)
        vbox.addWidget(w2)
        vbox.addWidget(w3)
        hbox0 = QHBoxLayout(w0)
        hbox01 = QHBoxLayout(w01)
        hbox1 = QHBoxLayout(w1)
        hbox2 = QHBoxLayout(w2)
        hbox3 = QHBoxLayout(w3)

        self.label_item_number = QLabel('显示数量(个)')
        self.label_select_word = QLabel('包含关键字')
        self.label_function_consuming_time = QLabel('函数耗时(s)')
        self.label_sentence_max_consuming_time =  QLabel('语句最大耗时(s)')
        self.label_sentence_max_time =  QLabel('语句最大次数(个）')

        self.lineedit_item_number  = QDoubleSpinBox()
        self.lineedit_item_number.setValue(20)
        self.lineedit_select_word = QLineEdit()
        self.spinbox_function_consuming_time1  = QDoubleSpinBox()
        self.spinbox_function_consuming_time1.setValue(0)
        self.spinbox_function_consuming_time2  = QDoubleSpinBox()
        self.spinbox_function_consuming_time2.setValue(0)
        self.spinbox_sentence_max_consuming_time1  = QDoubleSpinBox()
        self.spinbox_sentence_max_consuming_time1.setValue(0)
        self.spinbox_sentence_max_consuming_time2  = QDoubleSpinBox()
        self.spinbox_sentence_max_consuming_time2.setValue(0)
        self.spinbox_sentence_max_time1  = QDoubleSpinBox()
        self.spinbox_sentence_max_time1.setValue(0)
        self.spinbox_sentence_max_time1.setMaximum(10000000)
        self.spinbox_sentence_max_time2  = QDoubleSpinBox()
        self.spinbox_sentence_max_time2.setMaximum(10000000)
        self.spinbox_sentence_max_time2.setValue(0)

      
        hbox0.addWidget(self.label_item_number)        
        hbox0.addWidget(self.lineedit_item_number)   

        hbox01.addWidget(self.label_select_word)
        hbox01.addWidget(self.lineedit_select_word)

        hbox1.addWidget(self.label_function_consuming_time)        
        hbox1.addWidget(self.spinbox_function_consuming_time1)   
        hbox1.addWidget(self.spinbox_function_consuming_time2)   

        hbox2.addWidget(self.label_sentence_max_consuming_time)        
        hbox2.addWidget(self.spinbox_sentence_max_consuming_time1)  
        hbox2.addWidget(self.spinbox_sentence_max_consuming_time2)  
              
        hbox3.addWidget(self.label_sentence_max_time)        
        hbox3.addWidget(self.spinbox_sentence_max_time1)        
        hbox3.addWidget(self.spinbox_sentence_max_time2)        

    def _init_middle(self):
        """初始化排序面板"""
        vbox = QVBoxLayout(self.middle_sort_panel)

        label_title_middle = QLabel('排序')
        vbox.addWidget(label_title_middle)

        self.radio_function_consuming_time = QRadioButton('函数耗时',self.middle_sort_panel)
        self.radio_sentence_max_consuming_time = QRadioButton('语句最大耗时',self.middle_sort_panel)
        self.radio_sentence_max_time = QRadioButton('语句最大次数',self.middle_sort_panel)

        vbox.addWidget(self.radio_function_consuming_time)
        vbox.addWidget(self.radio_sentence_max_consuming_time)
        vbox.addWidget(self.radio_sentence_max_time)
